package com.dhwl.hsh.merchant.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.dhwl.hsh.lib.bean.resp.HtmlUrlResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.presenter.MainPresenter;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 协议条款
 *
 * @author Derek.Kidd
 * @time 2016/5/4 18:53
 */
public class ProtocolListActivity extends BaseActivity implements MvpView {

    @Bind(R.id.layout_pro_1)
    RelativeLayout layoutPro1;
    @Bind(R.id.layout_pro_2)
    RelativeLayout layoutPro2;


    HtmlUrlResp htmlUrlResp;

    MainPresenter mainPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_protocol_list;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {


    }

    @Override
    protected void initData() {

        layoutPro1.setEnabled(false);
        layoutPro2.setEnabled(false);
        ReservoirUtils reservoirUtils = new ReservoirUtils();

        try {
            htmlUrlResp = reservoirUtils.getSync("key_html", HtmlUrlResp.class);
            if (null == htmlUrlResp) {
                mainPresenter = new MainPresenter();
                mainPresenter.attachView(this);
                mainPresenter.getHtmlPage();
            } else {
                layoutPro1.setEnabled(true);
                layoutPro2.setEnabled(true);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @OnClick({R.id.layout_pro_1, R.id.layout_pro_2})
    public void onClick(View view) {
        Intent intent = new Intent(this, WebViewActivity.class);
        switch (view.getId()) {
            case R.id.layout_pro_1:
                intent.putExtra(WebViewActivity.WEBVIEW_TITLE, getString(R.string.app_merchant_pro));
                intent.putExtra(WebViewActivity.WEBVIEW_URL, htmlUrlResp.getContractUrl());
                break;
            case R.id.layout_pro_2:
                intent.putExtra(WebViewActivity.WEBVIEW_TITLE, getString(R.string.app_service_pro));
                intent.putExtra(WebViewActivity.WEBVIEW_URL, htmlUrlResp.getServicesUrl());
                break;
        }
        startActivity(intent);
    }

    @Override
    public void onFailure(HshException e) {

    }

    @Override
    public void onSuccess(Object object) {
        htmlUrlResp = (HtmlUrlResp) object;
        layoutPro1.setEnabled(true);
        layoutPro2.setEnabled(true);
    }
}
